<template>
  <div class="bottom-bar">
    <div class="check-content">
      <check-button :is-checked="isSelectAll" @click.native="checkClick" class="check-button"/>
      <span>全选</span>
    </div>
    <div class="price">合计：{{totalPrice}}</div>
    <div class="calculate" @click="calcClick">去计算({{checkLength}})</div>
  </div>
</template>

<script>
  import CheckButton from 'components/content/checkButton/CheckButton'
  import { mapGetters } from 'vuex'

  export default {
    name: 'CartBottomBar',
    components: {
      CheckButton
    },
    computed: {
      ...mapGetters(['cartList']),
      totalPrice() {
        return '￥' + this.cartList.filter(item => {
          return item.checked;
        }).reduce((preValue, item) => {
          return preValue + item.price * item.count;
        }, 0).toFixed(2);
      },
      checkLength() {
        return this.cartList.filter(item => item.checked).length;
      },
      isSelectAll() {
        if (this.cartList.length === 0) return false;

        // return !(this.cartList.filter(item => !item.checked).length);

        return !this.cartList.find(item => !item.checked);

        // for (let item of this.cartList) {
        //   if (!item.checked) {
        //     return false;
        //   }
        // }
        // return true;
    },
    methods: {
      checkClick() {
        if (this.isSelectAll) {
          // 全部选中时点击->全部不选中
          this.cartList.forEach(item => item.checked = false)
        } else {
          // 部分、全部不选中时点击->全部选中
          this.cartList.forEach(item => item.checked = true)
        }

        // 会互相影响
        // this.cartList.forEach(item => item.checked = !this.isSelectAll)
      },
      calcClick() {
        if (!this.isSelectAll) {
          this.$toast.show('请选择购买的商品', 2000)
        }
      }
    }
  }
}
</script>

<style scoped>
  .bottom-bar {
    display: flex;
    position: relative;
    height: 40px;
    line-height: 40px;
    background-color: #eee;
    font-size: 14px;
  }

  .check-content {
    display: flex;
    align-items: center;
    width: 60px;
    margin-left: 10px;
  }

  .check-button {
    width: 20px;
    height: 20px;
    line-height: 20px;
    margin-right: 5px;
  }

  .price {
    flex: 1;
    margin-left: 20px;
  }

  .calculate {
    width: 90px;
    text-align: center;
    background-color: #ff0000;
    color: #fff;
  }
</style>
